﻿#include "setup1wind.h"
#include "ui_setup1wind.h"
#include <mainwindow.h>
extern intListener doorStatus;
QString filep;
QString isc="1";

setup1Wind::setup1Wind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setup1Wind)
{
    ui->setupUi(this);
    seldoutpath();

    ui->defaultFileExportMode->setChecked(true);

    //ui->defaultFilePath->setEnabled(false);

    ui->defaultFilePath->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->defaultFilePath->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    connect(&doorStatus, &intListener::valueChanged,this,&setup1Wind::doorStatusChanged,Qt::QueuedConnection);
#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 18); // 创建一个QFont对象，设置字体名称和默认大小

            // 使用setFont方法设置字体，传入QFont对象
            ui->CancelBtn->setFont(font);
            ui->SaveBtn->setFont(font);
            ui->label->setFont(font);
            ui->label_2->setFont(font);
            ui->defaultFilePath->setFont(font);
            ui->defaultFileExportMode->setFont(font);


            // 如果需要为其他按钮设置不同的大小，可以创建新的QFont对象
            QFont smallerFont(familyName, 16);

            ui->returnBtn->setFont(familyName);
            ui->pushButton_9->setFont(familyName);
            ui->label_3->setFont(familyName);

        }
    }
#endif
}
void setup1Wind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
}

setup1Wind::~setup1Wind()
{
    delete ui;
}

void setup1Wind::showEvent(QShowEvent *event)
{

    tcpClient = TcpClient::getInstance();
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
    event->accept();
}

void setup1Wind::on_defaultFilePathBtn_clicked()
{
#ifdef Q_OS_ANDROID
    //ui->defaultFilePath->setEnabled(false);
    DialogFileManager dfm(1);
    dfm.exec();
    QString result = dfm.getResult();
    if(result != "0" && result != ""){
        ui->defaultFilePath->clear();
        ui->defaultFilePath->setText(result);
    }
#else
    filePath = QFileDialog::getExistingDirectory(this, "选择文件夹", "/path/to/default/directory");
    if(!filePath.isEmpty()){
        ui->defaultFilePath->clear();
        ui->defaultFilePath->setText(filePath);
    }
#endif
}

void setup1Wind::on_defaultFileExportMode_clicked(bool checked)
{
    if(checked){
        isc="1";
    }else {
        isc="0";
    }
}

void setup1Wind::on_returnBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup1Wind::on_SaveBtn_clicked()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString lineEditText = ui->defaultFilePath->toPlainText();
    qDebug() << lineEditText;
    lineEditText.replace("\\", "\\\\");
    qDebug() << lineEditText;
    QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2'").arg(ui->defaultFilePath->toPlainText(),"100");
    query.exec(sql);

    QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "保存成功  ", QMessageBox::Yes, this);
    box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
    box->exec();
    filep=ui->defaultFilePath->toPlainText();
}

void setup1Wind::on_CancelBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}


void setup1Wind::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
        ui->pushButton_9->setText("        关闭仓门");
    }
    else
    {
        doorStatus.set(0);
        ui->pushButton_9->setText("        打开仓门");
    }
}
// cnr 6.27 导出路径读取
void setup1Wind::seldoutpath(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=100";
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        filePath=column1Value;
    }
    qDebug()<<filePath;
    if(!filePath.isEmpty()){
        ui->defaultFilePath->setText(filePath);
    }
    filep = filePath;
}
